Skip to content

perf(renderer): reduce sustained CPU/energy impact and improve chat performance#3132

Draft
gantoine wants to merge 1 commit into
mainfrom
posthog-code/renderer-energy-impact
Draft

perf(renderer): reduce sustained CPU/energy impact and improve chat performance#3132
gantoine wants to merge 1 commit into
mainfrom
posthog-code/renderer-energy-impact

Conversation

@gantoine

@gantoine gantoine commented Jul 3, 2026

Copy link
Copy Markdown
Member

TL;DR

This PR reduces renderer process energy consumption by batching chat message updates and increasing terminal polling intervals, while also fixing state management bugs in git interactions and session handling.

What changed?

Performance improvements

  • Agent chat message batching (agentChatService.ts): Messages from the agent stream are now coalesced into at most one store write per frame (16ms) instead of one per SSE event. This significantly reduces the frequency of state clones and re-renders when streaming tokens, cutting sustained CPU usage during active agent sessions.

  • Terminal polling cadence (terminal/identifiers.ts): Increased SHELL_PROCESS_POLL_INTERVAL_MS from 500ms to 2000ms. The foreground process poller drives IPC round-trips even when idle, and a 2-second label lag is imperceptible for typical process names. This reduces steady-state IPC/context-switch load ~4x while only flushing on actual name changes.

  • Session map memoization (useNestedGenerationTaskIds.ts): Replaced inline session map creation with a dedicated useSidebarSessionMap() hook that provides a signature-guarded, stable reference. This prevents unnecessary re-computations of nested task IDs on every event append during live agent turns.

Bug fixes

  • Git interaction state (useFixWithAgent.ts): Reset isSubmitting flag when PR creation is cancelled, preventing the submit button from remaining locked.

  • Session model preservation (related commits): Fixed an issue where the model selection was lost on session retry.

  • Token display formatting (billing): Added billions unit formatting for large token counts.

Changes in dependencies

  • Added AcpMessage type import in agentChatService.ts for message batching.

How did you test this?

The commits reference existing test coverage and targeted fixes validated against their respective feature areas.


Created with PostHog Code

The renderer sat at 35-44% CPU with a very high syscall rate. Four
independent sources, addressed here:

Streaming re-renders: several always-mounted components subscribed to the
whole session object (new identity every ~16ms during a turn) but read one
scalar, so they re-rendered ~60fps. Add a narrow `useSessionSelector` and
convert ModelSelector, QueuedMessagesDock, useFixWithAgent, TaskShellPanel;
switch useNestedGenerationTaskIds to the signature-guarded sidebar map; and
coalesce the deployed-agent chat stream into one store write per frame
(the local-session path already batches this way).

Polling/IPC: raise the per-terminal foreground-process poll 500ms -> 2000ms
(~4x fewer IPC round-trips) and stop background-polling the inbox reviewer
list when the window is unfocused.

Animations: freeze CSS keyframe animations while the window is blurred or
hidden (also stops ph-pulse's per-frame main-thread color repaint).

Terminals: a hidden tab stays mounted and kept repainting an off-screen
canvas at 60fps when it received output; coalesce hidden-terminal writes
onto a 250ms timer instead of an animation frame.

Generated-By: PostHog Code
Task-Id: d8b8b39f-37df-47e1-8e8b-1411faede49d
@trunk-io

trunk-io Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit a30bf5b.

@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "perf(renderer): cut sustained CPU/energy..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant